package com.matt.controller.group;

import com.matt.commons.dto.sso.StaffDTO;
import com.matt.commons.exception.BusinessException;
import com.matt.commons.model.StatusResponse;
import com.matt.commons.page.PagerQuery;
import com.matt.domain.ComplaintRecord;
import com.matt.foundation.constant.PermissionConstant;
import com.matt.foundation.constant.RequestConstant;
import com.matt.foundation.model.ErrorCode;
import com.matt.foundation.model.MemVerificationCache;
import com.matt.foundation.utils.ModelUtil;
import com.matt.foundation.utils.annotation.RequiredPermissions;
import com.matt.service.ComplaintRecordService;
import com.matt.service.remoteCall.DTO.SsoAreaDTO;
import com.matt.service.remoteCall.SsoCallService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/web/group/complaintRecord")
@Slf4j
@Api(tags = "Web集团 - 投诉")
public class WebGroupComplaintRecordController {


    @Autowired
    private MemVerificationCache cache;

    @Autowired
    private SsoCallService ssoCallService;

    @Autowired
    private ComplaintRecordService complaintRecordService;

    /**
     * 删除
     * @param ticket
     * @param complaintRecordId
     * @return
     */
    @DeleteMapping("")
    @ApiOperation(value = "删除一条记录")
    @RequiredPermissions({PermissionConstant.TEACH_COMPLAINT_DELETE})
    public StatusResponse deleteComplaintRecord(@RequestHeader(RequestConstant.TICKET) String ticket, String complaintRecordId) {
        StaffDTO staff = cache.getStaff(ticket);

        if(null == staff)
            throw new BusinessException(ErrorCode.PROMISSION_DELINED, "权限不足,用户无角色");

        String staffName = staff.getRealName();

        ComplaintRecord record =  complaintRecordService.deleteComplaintRecord(staff.getId(), staffName, complaintRecordId);

        return StatusResponse.success(ModelUtil.mapOf("noticeId", record.getId(), "deleteStatus", record.getDeleted()));
    }

    /**APP
     * 查询投诉列表
     */
    @GetMapping("")
    @ApiOperation(value = "获取集团投诉记录列表")
    public StatusResponse getAppComplaintRecord(@RequestHeader(value = RequestConstant.TICKET, required = false) String ticket,String  roleCode,String queryRoleCode,Boolean isReply, PagerQuery query){

        List<SsoAreaDTO> areaDTOList = ssoCallService.getGroupAllArea(ticket);
        List<String> ids = areaDTOList.stream().map(o->o.getId()).collect(Collectors.toList());

        Page<ComplaintRecord> result = complaintRecordService.getComplaintRecord(ids, roleCode,
                queryRoleCode, isReply, query.getPageNum(), query.getPageSize());

        Map<String, SsoAreaDTO> areaCache = areaDTOList.stream().collect(Collectors.toMap(SsoAreaDTO::getId, o->o));
        if (areaCache.size() > 0 ){
            result.getContent().stream().forEach(o->{
                SsoAreaDTO dto = areaCache.get(o.getAreaId());
                if (dto != null) {
                    o.setAreaName(dto.getAreaName());
                }
            });
        }

        return StatusResponse.success(result);
    }


    /**
     * 回复投诉
     * @param ticket
     * @param complaintRecordId
     *@param replyContent 回复内容
     * @return
     */
    @PostMapping("/reply")
    @ApiOperation(value = "回复内容")
    @RequiredPermissions(PermissionConstant.TEACH_COMPLAINT_REPLY)
    public StatusResponse deleteNotice(@RequestHeader(RequestConstant.TICKET) String ticket, String complaintRecordId, String replyContent) {

        StaffDTO staff = cache.getStaff(ticket);

        String staffName = staff.getRealName();

        ComplaintRecord record =  complaintRecordService.replyComplaintRecord(staff.getId(), staffName, complaintRecordId, replyContent);
        return StatusResponse.success(record);
    }

}
